/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.application;

import java.util.Hashtable;
import java.util.Locale;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
 * Class holds client browser info. Accessible throw WebContext
 */

public class BrowserInfo {
    protected HttpServletRequest request;
    protected HttpSession session;
    protected Locale locale; // Locale-Objekt mit den aktuellen
                             // Spracheinstellungen
    protected String company; // Firmenname des Herstellers
    protected String language = "en"; // Sprachcode Standard
    protected String mainVersion; // Hauptversion
    protected String minorVersion; // Unterversion
    protected String name; // Bezeichnung des Browsers
    protected String os; // Betriebssystem
    protected String userAgent;
    protected String version; // Version
    private Hashtable supportedLanguages; // Untersttzte Sprachen

    public BrowserInfo(HttpServletRequest request) {
        this.initialize();
        this.request = request;
        this.session = request.getSession();

        this.setUserAgent(request.getHeader("User-Agent"));
        this.setCompany();
        this.setName();
        this.setVersion();
        this.setMainVersion();
        this.setMinorVersion();
        this.setOs();
        this.setLanguage();
        this.setLocale();
    }

    /**
     * Liefert den Firmennamen des Herstellers des verwendeten Browsers.
     */
    public String getCompany() {
        return this.company;
    }

    public String getLanguage() {
        return this.language;
    }

    /**
     * Liefert ein Locale-Objekt mit der Sprach-Prferenz des verwendeten Browsers
     */
    public Locale getLocale() {
        return this.locale;
    }

    /**
     * Liefert die Hauptversionsnummer des verwendeten Browsers.
     */
    public String getMainVersion() {
        return this.mainVersion;
    }

    /**
     * Liefert die Unterversionsnummer des verwendeten Browsers.
     */
    public String getMinorVersion() {
        return this.minorVersion;
    }

    /**
     * Liefert den Namen des verwendeten Browsers.
     */
    public String getName() {
        return this.name;
    }

    /**
     * Liefert den Namen des Betriebssystems.
     */
    public String getOs() {
        return this.os;
    }

    public void setUserAgent(String httpUserAgent) {
        this.userAgent = httpUserAgent.toLowerCase();
    }

    /**
     * Liefert die Versionsnummer des verwendeten Browsers.
     */
    public String getVersion() {
        return this.version;
    }

    public void initialize() {
        this.supportedLanguages = new Hashtable(2);
        this.supportedLanguages.put("en-us", "");
        this.supportedLanguages.put("de", "");
    }

    private void setCompany() {
        if (userAgent.indexOf("msie") > -1) {
            company = "Microsoft";
        } else if (userAgent.indexOf("opera") > -1) {
            this.company = "Opera Software";
        } else if (userAgent.indexOf("mozilla") > -1) {
            this.company = "Netscape Communications";
        } else {
            this.company = "unknown";
        }
    }

    private void setLanguage() {
        String prefLanguage = request.getHeader("Accept-Language");

        if (prefLanguage != null) {
            String language = null;
            StringTokenizer st = new StringTokenizer(prefLanguage, ",");
            int elements = st.countTokens();
            for (int idx = 0; idx < elements; idx++) {
                language =st.nextToken();
                if (this.supportedLanguages.containsKey(language)) {
                    this.language = this.parseLocale(language);
                }
            }
        }
    }

    private void setLocale() {
        this.locale = new Locale(this.language, "");
    }

    private void setMainVersion() {
        int i = this.version.indexOf(".");

        if (i != -1) {
            this.mainVersion = this.version.substring(0, i);
        }
    }

    private void setMinorVersion() {
        this.minorVersion = this.version.substring(this.version.indexOf(".") + 1).trim();
    }

    private void setName() {
        if (this.company == "Microsoft") {
            this.name = "Microsoft Internet Explorer";
        } else if (this.company == "Netscape Communications") {
            this.name = "Netscape Navigator";
        } else if (this.company == "Operasoftware") {
            this.name = "Operasoftware Opera";
        } else {
            this.name = "unknown";
        }
    }

    private void setOs() {
        if (this.userAgent.indexOf("win") > -1) {
            if ((this.userAgent.indexOf("windows 95") > -1) || (this.userAgent.indexOf("win95") > -1)) {
                this.os = "Windows 95";
            }

            if ((this.userAgent.indexOf("windows 98") > -1) || (this.userAgent.indexOf("win98") > -1)) {
                this.os = "Windows 98";
            }

            if ((this.userAgent.indexOf("windows nt") > -1) || (this.userAgent.indexOf("winnt") > -1)) {
                this.os = "Windows NT";
            }

            if ((this.userAgent.indexOf("win16") > -1) || (this.userAgent.indexOf("windows 3.") > -1)) {
                this.os = "Windows 3.x";
            }
        }
    }

    private void setVersion() {
        int tmpPos;

        if (this.company == "Microsoft") {
            String str = this.userAgent.substring(userAgent.indexOf("msie") + 5);
            tmpPos = str.indexOf(";");

            if (tmpPos != -1) {
                version = str.substring(0, tmpPos);
            }
        } else {
            this.version = "1";

            /*
                String tmpString = (this.userAgent.substring(tmpPos = (this.userAgent.indexOf("/")) + 1, tmpPos + this.userAgent.indexOf(" "))).trim();
                tmpPos = tmpString.indexOf(" ");
                if(tmpPos!=-1)
                   this.version = tmpString.substring(0, tmpPos);
                   */
        }
    }

    /*
     * Hilfsfunktion fr setLanguage().
     */
    private String parseLocale(String language) {
        StringTokenizer st = new StringTokenizer(language, "-");

        if (st.countTokens() == 2) {
            return st.nextToken();
        } else {
            return language;
        }
    }
}
